#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# author    : Xiangwei Wang
# email     : wangxw-cn@qq.com
# datetime  : 2021/4/29 17:03

"""
提取预测数据中的时间特征
"""

import time
import json
import pandas as pd
import chinese_calendar
# chinese_calendar: https://pypi.org/project/chinesecalendar/
import datetime

train_data_file = "../../Data/weibo_predict_data.txt"
# train_slim_data_file = "../Data/train_slim_data.txt"  # 测试程序用的少量数据

time_feature_1_file = "../../Data/predict/time_feature_1.json"  # 时间特征的结果

users = {}

def get_json():
    with open(train_data_file, 'rb') as f:
        line_cnt = 0
        lines = f.readlines()
        for l in lines:
            line_cnt += 1
            if line_cnt % 10000 == 0:
                print(f"已经处理 {line_cnt} 行数据")
            line = l.decode('utf-8')
            # line = line.strip()
            fields = line.split("\t", 3)  # 分解为6个字段，限制为6是防止博文内容含有\t
            if len(fields) != 4:
                print(f"line {line_cnt} has error: {line}   {fields}")
            else:
                uid = fields[0]
                mid = fields[1]
                forward_num = 0
                comment_num = 0
                like_num = 0


                thisTime = time.strptime(fields[2], '%Y-%m-%d %H:%M:%S')
                thisDate = datetime.date(thisTime.tm_year, thisTime.tm_mon, thisTime.tm_mday)
                weekday = thisTime.tm_wday+1  # 周几，1-7
                yearday = thisTime.tm_yday  # 一年中第几天，1-366
                is_workday = chinese_calendar.is_workday(thisDate)
                is_holiday, holiday_name = chinese_calendar.get_holiday_detail(thisDate)
                is_weekend = False
                if weekday > 5:
                    is_weekend = True
                hour = thisTime.tm_hour

                timestamp = time.mktime(thisTime)  # 字符串->时间元组->时间戳（10位浮点）

                if uid in users:
                    users[uid]["blogs"][mid] = {
                        "timestamp": timestamp,
                        "raw_time": fields[2],
                        "time_weekday": weekday,
                        "time_yearday": yearday,
                        "time_hour": hour,
                        "time_is_workday": is_workday,
                        "time_is_holiday": is_holiday,
                        "time_is_weekend": is_weekend,
                        "time_holiday_name": holiday_name,

                        "forward_num": forward_num,
                        "comment_num": comment_num,
                        "like_num": like_num,
                    }
                else:
                    users[uid] = {
                        "blogs": {
                            mid: {
                                "timestamp": timestamp,
                                "raw_time": fields[2],
                                "time_weekday": weekday,
                                "time_yearday": yearday,
                                "time_hour": hour,
                                "time_is_workday": is_workday,
                                "time_is_holiday": is_holiday,
                                "time_is_weekend": is_weekend,
                                "time_holiday_name": holiday_name,

                                "forward_num": forward_num,
                                "comment_num": comment_num,
                                "like_num": like_num,
                            }
                        }
                    }
                # print(users[uid])
        print(f"读取完成，共计 {line_cnt} 行")

        # ======= 写入json ===========：
        jsonStr = json.dumps(users, ensure_ascii=False)
        with open(time_feature_1_file, "w", encoding='utf-8') as f1:
            f1.write(jsonStr)

if __name__ == "__main__":
    get_json()